<?php require_once(CONTROLLER_PATH . 'admin/Admin_master_controller.php');
class Product extends Admin_master_controller {
	
	protected $_page_uri = "product";
	
	function __construct() {
		parent::__construct();
		$this->load->model('DNN_model');
		$this->load->model('Category_model');
		$this->load->model('Product_model');
		$this->_page_uri = admin_url() . $this->_page_uri;
		$this->view_data['page_uri'] = $this->_page_uri;
		$this->view_data['active_product'] = "current_page_item";
		$this->view_data['moduleTitle'] = $this->lang->line('product-manager');
		// type=1 is Product
		$this->view_data['categories'] = $this->Category_model->get_list_by_type(1)->result();
	}
	function index($offset = 0) {
		$this->view_data['view'] = ADMIN_PATH . "product_list";
		$this->view_data['page_title'] = $this->lang->line('product-list');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$data_list = $this->Product_model->get_paged_list(ADMIN_ITEM_PER_PAGE, $offset)->result();
		$this->view_data['pagination'] = $this->_render_paging($this->Product_model->count_all(), $offset);
		$this->view_data['table'] = $this->_render_table($data_list, $offset);
		$this->view_data['offset'] = $offset;
		$this->render_page($this->view_data);
	}
	function add($offset = 0) {
		// set common properties
		$this->view_data['view'] = ADMIN_PATH . "product_edit";
		$this->view_data['page_title'] = $this->lang->line('product-add');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$this->view_data['action'] = $this->_page_uri . '/add/' . $offset;
		$this->view_data['link_back'] = $this->_page_uri . '/index/' . $offset;
		// set validation properties
		$this->_set_fields();
		$this->_set_rules();
		$this->view_data['error'] = '';
		// run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else {
			// process upload
			$config['upload_path'] = UPLOAD_PATH;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['encrypt_name']	= TRUE;
			$this->load->library('upload', $config);
			$img = '';
			$thumb = '';
			if(isset($_FILES['image']['name'][0])) {
				// upload image
				if ( ! $this->upload->do_upload('image')) {
					$this->view_data['error'] = $this->upload->display_errors();
				} else {
					$img = $this->upload->data();
					// create thumb
					$this->_resize($img['full_path'], 100, 100);
					$img = $img['file_name'];
				}
			}
			if(isset($_FILES['image_thumb']['name'][0])) {
				// upload image_thumb
				if ( ! $this->upload->do_upload('image_thumb')) {
					$this->view_data['error'] = $this->upload->display_errors();
				} else {
					$thumb = $this->upload->data();
					// create thumb
					$this->_resize($thumb['full_path'], 50, 50);
					$thumb = $thumb['file_name'];;
				}
			}
			//save data
			$data_arr = array (
				'title_'.$this->_language => $this->input->post('title'),
				'summary_'.$this->_language => $this->input->post('summary'),
				'body_'.$this->_language => $this->input->post('body'),
				'image' => $img,
				'image_thumb' => $thumb,
				'image_description_'.$this->_language => $this->input->post('image_description'),
				'category' => $this->input->post('category'),
				'created_date' => date('Y-m-d H:i:s'),
			);
			$id = $this->Product_model->add($data_arr);
			$this->validation->id = $id;
			$this->validation->image = $img;
			$this->validation->image_thumb = $thumb;
			$this->view_data['message'] = $this->lang->line('add-success');
		}
		$this->render_page($this->view_data);
	}
	
	function edit($id = 0, $offset) {
		//set common properties
		$this->view_data['view'] = ADMIN_PATH . "product_edit";
		$this->view_data['page_title'] = $this->lang->line('product-edit');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$this->view_data['action'] = $this->_page_uri . '/edit/' . $id . "/" . $offset;
		$this->view_data['link_back'] = $this->_page_uri . '/index/' . $offset;
		
		//set validation properties
		$this->_set_fields();
		$this->_set_rules();
		$this->view_data['error'] = '';
		//run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else {
			//process upload
			$config['upload_path'] = UPLOAD_PATH;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['encrypt_name']	= TRUE;
			$this->load->library('upload', $config);
			
			$id = $this->input->post('id');
			$data_arr = $this->Product_model->get_by_id($id)->row();
			$img = $data_arr->image;
			$thumb = $data_arr->image_thumb;
			if(isset($_FILES['image']['name'][0])) {
				// upload image
				if ( ! $this->upload->do_upload('image')) {
					$this->view_data['error'] = $this->upload->display_errors();
				} else {
					$img = $this->upload->data();
					// create thumb
					$this->_resize($img['full_path'], 100, 100);
					$img = $img['file_name'];
				}
			}
			if(isset($_FILES['image_thumb']['name'][0])) {
				// upload image_thumb
				if ( ! $this->upload->do_upload('image_thumb')) {
					$this->view_data['error'] = $this->upload->display_errors();
				} else {
					$thumb = $this->upload->data();
					// create thumb
					$this->_resize($thumb['full_path'], 50, 50);
					$thumb = $thumb['file_name'];;
				}
			}
			//save data
			$data_arr = array (
				'title_'.$this->_language => $this->input->post('title'),
				'summary_'.$this->_language => $this->input->post('summary'),
				'body_'.$this->_language => $this->input->post('body'),
				'image' => $img,
				'image_thumb' => $thumb,
				'image_description_'.$this->_language => $this->input->post('image_description'),
				'category' => $this->input->post('category'),
			);
			$this->Product_model->update($id, $data_arr);
			$this->view_data['message'] = $this->lang->line('edit-success');
			$this->validation->image = $img;
			$this->validation->image_thumb = $thumb;
		}
		// refill form values
		$data_arr = $this->Product_model->get_by_id($id)->row();
		$this->validation->id = $id;
		$this->validation->title = $data_arr->title;
		$this->validation->summary = $data_arr->summary;
		$this->validation->image = $data_arr->image;
		$this->validation->image_thumb = $data_arr->image_thumb;
		$this->validation->image_description = $data_arr->image_description;
		$this->validation->body = $data_arr->body;
		$_POST['category'] = $data_arr->category;
		
		$this->render_page($this->view_data);
	}
	
	function delete($id = 0, $offset = 0) {
		$this->Product_model->delete($id);
		redirect($this->_page_uri.'/index/' . $offset,'refresh');
	}
	//Private Functions
	function _render_table($data_list, $offset = 0) {
		// generate table data
		$this->load->library('table');
		$this->table->set_empty("&nbsp;");
		$this->table->set_heading($this->lang->line('no'), $this->lang->line('title'), $this->lang->line('summary'), $this->lang->line('category'), $this->lang->line('created-date'), $this->lang->line('actions'));
		$i = $offset;
		foreach ($data_list as $data_item) {
			$this->table->add_row(++$i, $data_item->title, $data_item->summary, $this->to_category($data_item->category), $data_item->created_date, 
				anchor($this->_page_uri.'/edit/'.$data_item->id . '/' . $offset ,$this->lang->line('edit') ,array('class'=>'update')).' '.
				anchor($this->_page_uri.'/delete/'.$data_item->id . '/' . $offset ,$this->lang->line('delete') ,array('class'=>'delete','onclick'=>"return confirm('".$this->lang->line('delete-confirm')."');"))
			);
		}
		return $this->table->generate();
	}
	function to_category($category) {
		$data_arr = $this->Category_model->get_by_id($category)->row();
		return $data_arr->name;
	}
	// validation fields
	function _set_fields() {
		$fields['id'] = 'id';
		$fields['title'] = 'title';
		$fields['summary'] = 'summary';
		$fields['body'] = 'body';
		$fields['image'] = 'image';
		$fields['image_thumb'] = 'image_thumb';
		$fields['image_description'] = 'image_description';
		$fields['category'] = 'category';
		$this->validation->set_fields($fields);
	}
	//validation rules
	function _set_rules() {
		$rules['id'] = 'trim';
		$rules['title'] = 'trim|required';
		$rules['summary'] = 'trim|required';
		$rules['body'] = 'trim|required';
		$rules['image'] = 'trim';
		$rules['image_thumb'] = 'trim';
		$rules['image_description'] = 'trim';
		$rules['category'] = 'trim';
		$this->validation->set_rules($rules);
		$this->validation->set_message('required', $this->lang->line('required'));
		$this->validation->set_message('isset', $this->lang->line('isset'));
		$this->validation->set_error_delimiters('<p class="error">', '</p>');
	}
}